/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ManageKendaraan;

/**
 *
 * @author rizky
 */

import Database.database;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Kendaraan {
    public Kendaraan(){
        
    }
//// Mengelola kendaraan
    @SuppressWarnings("static-access")
    public static String getListKendaraan()
    {
        //UnitKendaraan temp[] = new UnitKendaraan[20];
        String temp = "";
        //FILL TEMP dengan data dari dbase
        try {
            database connCars = new database();
            connCars.setQuery("SELECT * FROM mobil_charter ORDER BY id_jenis_mobil");
            temp += "<table width=\"407\" border=\"1\">" +
                    "    <tr>" +
                           " <th>ID kendaraan</th>" +
                           " <th>Jenis Kendaraan</th>" +
                           " <th>Hapus Kendaraan</th>" +
                        "</tr>";//connCars.result.next();
            while (connCars.result.next()) {
                    temp += "<tr>" +
                            "   <td>" +
                            "       cars-" + connCars.result.getInt("id_mobil") +
                            "   </td>";
                    String jenis = connCars.result.getString("id_jenis_mobil");
                    Statement typesql = connCars.conn.createStatement();
                    ResultSet types = typesql.executeQuery("SELECT * FROM jenis_mobil WHERE id_jenis_mobil = " + jenis );
                    types.next();
                    temp += "   <td>" + types.getString("jenis_mobil")+
                            "   </td>" +
                            "   <td>" +
                            "       <form method=\"post\" action=\"ServletHapusKendaraan\">" +
                            "           <input type=\"hidden\" name=\"id_mobil\" value=\""+connCars.result.getInt("id_mobil")+"\"> <input type=\"submit\" value=\"Hapus\">" +
                            "       </form>"+
                            "   </td>"+
                            "</tr>";
            }
            temp += "</table>";
        } catch (SQLException ex) {
            Logger.getLogger(Kendaraan.class.getName()).log(Level.SEVERE, null, ex);
        }
        return temp;
    }
    @SuppressWarnings("static-access")
    public int tambahKendaraan(String id_jenis_mobil){
        database db = new database();
        int result = -1;
        Statement stmt;
        try {
            stmt = db.conn.createStatement();
            result = stmt.executeUpdate("INSERT INTO mobil_charter(id_jenis_mobil)VALUES ('"+id_jenis_mobil+"')");
        } catch (SQLException ex) {
            Logger.getLogger(Kendaraan.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }
    @SuppressWarnings("static-access")
    public int hapusKendaraan(String id_mobil){
        database db = new database();
        int result = -1;
        try {
            Statement stmt = db.conn.createStatement();
            result = stmt.executeUpdate("DELETE FROM mobil_charter WHERE id_mobil = " + id_mobil);
        } catch (SQLException ex) {
            Logger.getLogger(Kendaraan.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }
/// Mengelola Jenis Kendaraan
    @SuppressWarnings("static-access")
    public static String getListJenisKendaraan(){
        String temp = "";
        try {
            database connCars = new database();
            connCars.setQuery("SELECT * FROM jenis_mobil ORDER BY jenis_mobil");
            temp += "<table width=\"500\" border=\"1\">" +
                    "    <tr><th>Jenis</th><th>Kapasitas</th><th>Tarif(Hari)</th><th>Tarif Supir(Hari)</th><th>Edit</th></tr>";
            while (connCars.result.next()) {
                temp += "<tr><td>" + connCars.result.getString("jenis_mobil") + "   </td>";
                temp += "   <td>" + connCars.result.getString("kapasitas") + "</td>" +
                        "   <td>" + connCars.result.getString("tarif_hari") + "</td>" + 
                        "   <td>" + connCars.result.getString("tarif_supir_hari") + "</td>" +
                        "   <td>" +
                        "       <form method=\"get\" action=\"ServletEditJenisKendaraan\">" +
                        "           <input type=\"hidden\" name=\"id_jenis_mobil\" value=\""+connCars.result.getInt("id_jenis_mobil")+"\"> " +
                        "           <input type=\"submit\" value=\"Edit\">" +
                        "       </form>"+
                        "</td>" +
                        "</tr>";
            }
            temp += "</table>";
        } catch (SQLException ex) {
            Logger.getLogger(Kendaraan.class.getName()).log(Level.SEVERE, null, ex);
        }
        return temp;
    }
     @SuppressWarnings("static-access")
    public int tambahJenisKendaraan(String jenis_mobil, String kapasitas, String tarif_charter, String tarif_supir){
        database db = new database();
        int result = -1;
        Statement stmt;
        try {
            stmt = db.conn.createStatement();
            result = stmt.executeUpdate("INSERT INTO jenis_mobil(jenis_mobil,kapasitas,tarif_hari,tarif_supir_hari)" +
                    "VALUES ('"+jenis_mobil+"','"+kapasitas+"','"+tarif_charter+"','"+tarif_supir+"')");
        } catch (SQLException ex) {
            Logger.getLogger(Kendaraan.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }
      @SuppressWarnings("static-access")
    public int editJenisKendaraan(String id_jenis_mobil, String jenis_mobil, String kapasitas, String tarif_charter, String tarif_supir){
        database db = new database();
        int result = -1;
        Statement stmt;
        try {
            stmt = db.conn.createStatement();
            result = stmt.executeUpdate("UPDATE jenis_mobil " +
                    "SET jenis_mobil ='"+jenis_mobil+"', " +
                        "kapasitas = "+kapasitas+", " +
                        "tarif_hari = "+tarif_charter+", " +
                        "tarif_supir_hari = "+tarif_supir+" " +
                    "WHERE id_jenis_mobil = "+id_jenis_mobil);
        } catch (SQLException ex) {
            Logger.getLogger(Kendaraan.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }
    @SuppressWarnings("static-access")
    public int hapusJenisKendaraan(String id_jenis_mobil){
        database db = new database();
        int result = -1;
        try {
            Statement stmt = db.conn.createStatement();
            result = stmt.executeUpdate("DELETE FROM jenis_mobil WHERE id_jenis_mobil = " + id_jenis_mobil);
        } catch (SQLException ex) {
            Logger.getLogger(Kendaraan.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }
}
